"use strict";
var app = require("../server.js");
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
module.exports = function (Chapter) {
  Chapter.getContentById = function (sectionId, cb) {
    var app = Chapter.app;
    var returnData = {};
    app.models.chapter.findById(sectionId, {}, function (err, res) {
      if (err) {
        console.log("获取id为" + sectionId + "的小说章节失败..." + err);
        cb(null, {
          code: -1,
          errMsg: "获取id为" + sectionId + "的小说章节失败",
        });
      } else {
        returnData.sectionContent = res.content;
        returnData.recentUpdateTime = res.create_time;
        cb(null, {
			code: 0,
			content: returnData,
			});
      }
    });
  };
  //register getContentById
  Chapter.remoteMethod("getContentById", {
    accepts: {
      arg: "sectionId",
      type: "string",
      description: "the id of a section",
    },
    returns: {
      arg: "data",
      type: "object",
      description: "返回的结果对象",
    },
    http: { path: "/getContentById", verb: "get" },
  });

  Chapter.getContentByHistory = function (userid, bookid, cb) {
    var app = Chapter.app;
    var returnData = {};
	
    app.models.user.findById(userid, {}, function (err, res) {
      if (err) {
        cb(null, {
          code: -1,
          errMsg: "获取用户信息失败",
        });
      } else {
        // 阅读进度
        let currentNum = 1;
        const readHistory = res.myBooks.filter(item => item.bookid === bookid)[0];
		console.log('已读文章数：'+readHistory.hasRead);
        if (readHistory) {
		//currentNum = Math.max(1, +readHistory.hasRead);
		currentNum+=readHistory.hasRead;
		}
        // 加载对应的章节
		console.log({num: currentNum,bookid});
		MongoClient.connect(
					url,
					(err, client) => {
						if(err){
							console.log('mongodb连接出错', err);
							return;
						}
						//console.log('mongodb连接成功');

						// 连接具体的数据库，比如myblog
						const db = client.db('myapp');

						// 使用集合 
						const userCollection = db.collection('chapter');

						// 查询
						userCollection.findOne({num: currentNum,bookid}, function(err, res2) {
						//console.log(res2);
						  if (err) {
							cb(null, {
							  code: -2,
							  errMsg: "获取章节信息失败",
							});
						  } else {
							cb(null, {
							  code: 0,
							  num: res2.num,
							  name:res2.name,
							  sectionid:res2._id,
							  content:res2.content,
							  
							});
						  }
						})
							
							// 关闭连接
							client.close();
						})
					}
				

      
    });
  };

  Chapter.remoteMethod("getContentByHistory", {
    accepts: [
      {
        arg: "userid",
        type: "string",
      },
      {
        arg: "bookid",
        type: "string",
      },
    ],
    returns: {
      arg: "data",
      type: "object",
      description: "返回的结果对象",
    },
    http: { path: "/getContentByHistory", verb: "get" },
  });
};
